home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / weightpaint_clean.py < prev    next >
Text File  |  2009-08-31  |  4KB  |  122 lines

  1. #!BPY
  2. """
  3. Name: 'Clean Weight...'
  4. Blender: 245
  5. Group: 'WeightPaint'
  6. Tooltip: 'Removed verts from groups below a weight limit.'
  7. """
  8.  
  9. __author__ = "Campbell Barton aka ideasman42"
  10. __url__ = ["www.blender.org", "blenderartists.org", "www.python.org"]
  11. __version__ = "0.1"
  12. __bpydoc__ = """\
  13.  
  14. Clean Weight
  15.  
  16. This Script is to be used only in weight paint mode,
  17. It removes very low weighted verts from the current group with a weight option.
  18. """
  19.  
  20. # ***** BEGIN GPL LICENSE BLOCK *****
  21. #
  22. # Script copyright (C) Campbell J Barton
  23. #
  24. # This program is free software; you can redistribute it and/or
  25. # modify it under the terms of the GNU General Public License
  26. # as published by the Free Software Foundation; either version 2
  27. # of the License, or (at your option) any later version.
  28. #    
  29. # This program is distributed in the hope that it will be useful,
  30. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  32. # GNU General Public License for more details.
  33. #
  34. # You should have received a copy of the GNU General Public License
  35. # along with this program; if not, write to the Free Software Foundation,
  36. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  37. #
  38. # ***** END GPL LICENCE BLOCK *****
  39. # --------------------------------------------------------------------------
  40.  
  41. from Blender import Scene, Draw, Object
  42. import BPyMesh
  43. def weightClean(me, PREF_THRESH, PREF_KEEP_SINGLE, PREF_OTHER_GROUPS):
  44.     
  45.     groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
  46.     act_group= me.activeGroup
  47.     
  48.     rem_count = 0
  49.     
  50.     if PREF_OTHER_GROUPS:
  51.         for wd in vWeightDict:
  52.             l = len(wd)
  53.             if not PREF_KEEP_SINGLE or l > 1:
  54.                 # cant use iteritems because the dict is having items removed
  55.                 for group in wd.keys():
  56.                     w= wd[group]
  57.                     if w <= PREF_THRESH:
  58.                         # small weight, remove.
  59.                         del wd[group]
  60.                         rem_count +=1
  61.                         l-=1
  62.                     
  63.                     if PREF_KEEP_SINGLE and l == 1:
  64.                         break
  65.     
  66.     else:
  67.         for wd in vWeightDict:
  68.             if not PREF_KEEP_SINGLE or len(wd) > 1:
  69.                 try:
  70.                     w= wd[act_group]
  71.                     if w <= PREF_THRESH:
  72.                         # small weight, remove.
  73.                         del wd[act_group]
  74.                         rem_count +=1
  75.                 except:
  76.                     pass
  77.     
  78.     # Copy weights back to the mesh.
  79.     BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
  80.     return rem_count
  81.  
  82.  
  83. def main():
  84.     scn= Scene.GetCurrent()
  85.     ob= scn.objects.active
  86.     
  87.     if not ob or ob.type != 'Mesh':
  88.         Draw.PupMenu('Error, no active mesh object, aborting.')
  89.         return
  90.     
  91.     me= ob.getData(mesh=1)
  92.     
  93.     PREF_PEAKWEIGHT= Draw.Create(0.001)
  94.     PREF_KEEP_SINGLE= Draw.Create(1)
  95.     PREF_OTHER_GROUPS= Draw.Create(0)
  96.     
  97.     pup_block= [\
  98.     ('Peak Weight:', PREF_PEAKWEIGHT, 0.005, 1.0, 'Remove verts from groups below this weight.'),\
  99.     ('All Other Groups', PREF_OTHER_GROUPS, 'Clean all groups, not just the current one.'),\
  100.     ('Keep Single User', PREF_KEEP_SINGLE, 'Keep verts in at least 1 group.'),\
  101.     ]
  102.     
  103.     if not Draw.PupBlock('Clean Selected Meshes...', pup_block):
  104.         return
  105.     
  106.     rem_count = weightClean(me, PREF_PEAKWEIGHT.val, PREF_KEEP_SINGLE.val, PREF_OTHER_GROUPS.val)
  107.     
  108.     # Run on entire blend file. usefull sometimes but dont let users do it.
  109.     '''
  110.     rem_count = 0
  111.     for ob in Object.Get():
  112.         if ob.type != 'Mesh':
  113.             continue
  114.         me= ob.getData(mesh=1)
  115.         
  116.         rem_count += weightClean(me, PREF_PEAKWEIGHT.val, PREF_KEEP_SINGLE.val, PREF_OTHER_GROUPS.val)
  117.     '''
  118.     Draw.PupMenu('Removed %i verts from groups' % rem_count)
  119.     
  120. if __name__=='__main__':
  121.     main()
  122.